home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEMP / GNU / bison / MfcalcDecm < prev    next >
Text File  |  1995-06-28  |  2KB  |  57 lines

  1. Mfcalc Decl
  2. Previous: <Multi-function Calc=>Multifunct> * Next: <Mfcalc Rules=>MfcalcRulf> * Up: <Multi-function Calc=>Multifunct>
  3.  
  4. #Wrap on
  5. {fH4}Declarations for {fCode}mfcalc{f}{f}
  6.  
  7. Here are the C and Bison declarations for the multi-function calculator.
  8.  
  9. #Wrap off
  10. #fCode
  11. %\{
  12. \#include <math.h>  \/\* For math functions, cos(), sin(), etc. \*\/
  13. \#include "calc.h"  \/\* Contains definition of `symrec'        \*\/
  14. %\}
  15. %union \{
  16. double     val;  \/\* For returning numbers.                   \*\/
  17. symrec  \*tptr;   \/\* For returning symbol-table pointers      \*\/
  18. \}
  19.  
  20. %token <val>  NUM        \/\* Simple double precision number   \*\/
  21. %token <tptr> VAR FNCT   \/\* Variable and Function            \*\/
  22. %type  <val>  exp
  23.  
  24. %right '='
  25. %left '-' '+'
  26. %left '\*' '\/'
  27. %left NEG     \/\* Negation--unary minus \*\/
  28. %right '^'    \/\* Exponentiation        \*\/
  29.  
  30. \/\* Grammar follows \*\/
  31.  
  32. %%
  33. #f
  34. #Wrap on
  35.  
  36. The above grammar introduces only two new features of the Bison language.
  37. These features allow semantic values to have various data types
  38. (\*Note <Multiple Types=>MultipleTy>: More Than One Value Type).
  39.  
  40. The {fCode}%union{f} declaration specifies the entire list of possible types;
  41. this is instead of defining {fCode}YYSTYPE{f}.  The allowable types are now
  42. double-floats (for {fCode}exp{f} and {fCode}NUM{f}) and pointers to entries in
  43. the symbol table.  \*Note <Union Decl=>UnionDecl>: The Collection of Value Types.
  44.  
  45. Since values can now have various types, it is necessary to associate a
  46. type with each grammar symbol whose semantic value is used.  These symbols
  47. are {fCode}NUM{f}, {fCode}VAR{f}, {fCode}FNCT{f}, and {fCode}exp{f}.  Their
  48. declarations are augmented with information about their data type (placed
  49. between angle brackets).
  50.  
  51. The Bison construct {fCode}%type{f} is used for declaring nonterminal symbols,
  52. just as {fCode}%token{f} is used for declaring token types.  We have not used
  53. {fCode}%type{f} before because nonterminal symbols are normally declared
  54. implicitly by the rules that define them.  But {fCode}exp{f} must be declared
  55. explicitly so we can specify its value type.  \*Note <Type Decl=>TypeDecl>: Nonterminal Symbols.
  56.  
  57.